package com.openbravo.possync;

import com.openbravo.basic.BasicException;
import com.openbravo.pos.forms.AppLocal;
import com.openbravo.pos.forms.DataLogicSystem;
import com.openbravo.pos.util.AltEncrypter;
import com.openbravo.pos.util.Base64Encoder;
import com.openbravo.ws.customers.Customer;
import com.openbravo.ws.customers.WebServiceImpl;
import com.openbravo.ws.customers.WebServiceImplServiceLocator;
import com.openbravo.ws.externalsales.ExternalSalesImpl;
import com.openbravo.ws.externalsales.ExternalSalesImplServiceLocator;
import com.openbravo.ws.externalsales.Order;
import com.openbravo.ws.externalsales.Product;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Properties;
import javax.xml.rpc.ServiceException;

public class ExternalSalesHelper
{
  private ExternalSalesImpl externalSales;
  private WebServiceImpl externalCustomers;
  private String m_sERPUser;
  private String m_sERPPassword;
  private String m_iERPId;
  private String m_iERPOrg;
  private String m_iERPPos;

  public ExternalSalesHelper(DataLogicSystem paramDataLogicSystem)
    throws BasicException, ServiceException, MalformedURLException
  {
    Properties localProperties = paramDataLogicSystem.getResourceAsProperties("openbravo.properties");
    if (localProperties == null)
      throw new BasicException(AppLocal.getIntString("message.propsnotdefined"));
    String str = localProperties.getProperty("url");
    if ((str == null) || (str.equals("")))
      throw new BasicException(AppLocal.getIntString("message.urlnotdefined"));
    str = str.trim();
    if (str.endsWith("/ExternalSales"))
      str = str.substring(0, str.length() - 14);
    this.externalSales = new ExternalSalesImplServiceLocator().getExternalSales(new URL(str + "/ExternalSales"));
    this.externalCustomers = new WebServiceImplServiceLocator().getWebService(new URL(str + "/WebService"));
    this.m_sERPUser = localProperties.getProperty("user");
    this.m_sERPPassword = localProperties.getProperty("password");
    if ((this.m_sERPUser != null) && (this.m_sERPPassword != null) && (this.m_sERPPassword.startsWith("crypt:")))
    {
      AltEncrypter localAltEncrypter = new AltEncrypter("key" + this.m_sERPUser);
      this.m_sERPPassword = localAltEncrypter.decrypt(this.m_sERPPassword.substring(6));
    }
    this.m_sERPPassword = getPasswordHash(this.m_sERPPassword);
    this.m_iERPId = localProperties.getProperty("id");
    this.m_iERPOrg = localProperties.getProperty("org");
    this.m_iERPPos = localProperties.getProperty("pos");
  }

  public Customer[] getCustomers()
    throws RemoteException
  {
    try
    {
      return this.externalCustomers.getCustomers(this.m_iERPId, this.m_sERPUser, this.m_sERPPassword);
    }
    catch (RemoteException localRemoteException)
    {
    }
    return new Customer[0];
  }

  public Product[] getProductsCatalog()
    throws RemoteException
  {
    try
    {
      return this.externalSales.getProductsPlusCatalog(this.m_iERPId, this.m_iERPOrg, this.m_iERPPos, this.m_sERPUser, this.m_sERPPassword);
    }
    catch (RemoteException localRemoteException)
    {
    }
    return this.externalSales.getProductsCatalog(this.m_iERPId, this.m_iERPOrg, this.m_iERPPos, this.m_sERPUser, this.m_sERPPassword);
  }

  public boolean uploadOrders(Order[] paramArrayOfOrder)
    throws RemoteException
  {
    return this.externalSales.uploadOrders(this.m_iERPId, this.m_iERPOrg, this.m_iERPPos, paramArrayOfOrder, this.m_sERPUser, this.m_sERPPassword);
  }

  private static String getPasswordHash(String paramString)
  {
    try
    {
      MessageDigest localMessageDigest = MessageDigest.getInstance("SHA-1");
      byte[] arrayOfByte = localMessageDigest.digest(paramString.getBytes("UTF-8"));
      return Base64Encoder.encode(arrayOfByte);
    }
    catch (NoSuchAlgorithmException localNoSuchAlgorithmException)
    {
      return null;
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
    }
    return null;
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.possync.ExternalSalesHelper
 * JD-Core Version:    0.6.0
 */